<?php


namespace App\Imports;

use App\Models\ArchiveManageVolume;
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithBatchInserts;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\WithColumnLimit;
use Maatwebsite\Excel\Concerns\WithLimit;
use Maatwebsite\Excel\Concerns\WithStartRow;

/**
 * Class BookTitleImport
 * @package App\Imports
 * 适用于 29号文件
 */
class ArchiveManageVolumeImport implements ToModel, WithChunkReading, WithStartRow, WithColumnLimit, WithBatchInserts, WithLimit, SkipsEmptyRows
{
    protected $chunkSize;
    protected $startRow;
    protected $endColumn;
    protected $batchSize;
    protected $tmpKey;
    protected $limit;
    protected $docArchiveInfoKey;
    protected $importCountKey;
    protected $projectId;
    protected $type;


    public function __construct($importCountKey, $projectId, $type)
    {
        $this->limit             = 10000; //只读取前10000行
        $this->chunkSize         = 100;  //将数据拆成小块读取
        $this->startRow          = 1;    //从第几行开始
        $this->endColumn         = 'D';  //指定到那一列为止
        $this->batchSize         = 100;  //批量写入的数据大小
        $this->docArchiveInfoKey = 'import_archive_manage_volumes';
        $this->importCountKey    = $importCountKey;
        $this->tmpKey            = 'data';
        $this->projectId         = $projectId;
        $this->type              = $type;
    }

    /**
     * 导入数据
     */
    public function model(array $row)
    {
        if ($row[0] && is_numeric($row[0])) {

            if ($this->type == 10) { //检查意见和整改报告缺少一个 文件编号 列 需单独处理
                ArchiveManageVolume::query()->updateOrCreate(
                    [ //查询条件
                        'project_id'  => $this->projectId,
                        'type'        => $this->type,
                        'file_name'   => format_table_label($row[1]),
                    ],
                    [ //数据
                        'serial_no'   => format_table_label($row[0]),
                        'remark'      => format_table_label($row[2]),
                    ]);
            }
            else {
                ArchiveManageVolume::query()->updateOrCreate(
                    [ //查询条件
                        'project_id'  => $this->projectId,
                        'type'        => $this->type,
                        'file_number' => format_table_label($row[1]),
                    ],
                    [ //数据
                        'serial_no'   => format_table_label($row[0]),
                        'file_number' => format_table_label($row[1]),
                        'file_name'   => format_table_label($row[2]),
                        'remark'      => format_table_label($row[3]),
                    ]);
            }
            redis()->incr($this->importCountKey); //导入的数据量加
        }
    }

    /**
     * 将数据拆成小块进行导入，减小内存占用
     *
     * @return int
     */
    public function chunkSize(): int
    {
        return $this->chunkSize;
    }

    /**
     * 直接过滤表头，第一块数据含表头
     *
     * @return int
     */
    public function startRow(): int
    {
        return $this->startRow;
    }

    /**
     * 指定到那一列为止，G代表读取到G列就不再读取
     *
     * @return string
     */
    public function endColumn(): string
    {
        return $this->endColumn;
    }

    /**
     * 批量写入的数据大小
     *
     * @return int
     */
    public function batchSize(): int
    {
        return $this->batchSize;
    }

    public function limit(): int
    {
        return $this->limit;
    }

}
